package org.rowan.infovis.soccer;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * PURPOSE: This class handles the details of parsing a data file (consisting of
 *          multiple players on a single team) and storing the fields for each player
 *          on the team
 * @author Ben Hample
 * @date 11-27-09
 */
public class FileParser {
    
    private TeamManager teamManager;
    private StateManager state;
    private Scanner reader;
    private int tableIndex;
    String fileLocation;

    /**
     * This is the constructor for the FileParser class
     */
    public FileParser(TeamManager tm, StateManager s) {
        fileLocation = "C:/Users/zone785/Documents/Book1.csv";
        tableIndex = 1;
        this.teamManager = tm;
        this.state = s;
    }

    /**
     * This method reads in the specified file and creates a list of players
     * from the contents of the file
     * @param f File to be read
     * @return ArrayList<PlayerData> list of players created from given file
     */
    public void readFileIn(File f) {
        if (f == null || !f.isFile() || !f.canRead()) {
            System.out.println("Internal File Selection: " + f.toString());
            f = new File(fileLocation);
        }
        try {
            System.out.println("External File Selection: " + f.toString());
            this.reader = new Scanner(f);
            reader.nextLine(); //Skip the Label Line
            while (reader.hasNextLine()) {
                String line = reader.nextLine(); //Get player data line

                System.out.println("PLAYER DATA: " + line);

                String[] playerInfo = line.split(","); //Split the player data

                //SET TEAM
                teamManager.setTeam(playerInfo[0]);

                //ADD PLAYER TO CURRENT TEAM
                teamManager.getCurrentTeam().addPlayer(playerInfo[1],playerInfo[2]); //Add Player using name

                //DEBUG
                System.out.println("PLAYER FIRST NAME: " + playerInfo[1]);
                System.out.println("PLAYER LAST NAME: " + playerInfo[2]);

                //teamManager.setTeamName(playerInfo[0]);    // SET TEAM NAME

                //SET PRECISION DATA (ROW 1)
                resetTableIndex();
                for (int i = 3; i <= 10; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setPrecisionData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getPrecisionData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("PRECISION DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET FLUENCY DATA (ROW 2)
                resetTableIndex();
                for (int i = 11; i <= 18; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setFluencyData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getFluencyData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("FLUENCY DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET COORDINATION DATA (ROW 3)
                resetTableIndex();
                for (int i = 19; i <= 26; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setCoordinationData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getCoordinationData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("COORDINATION DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET EASINESS DATA (ROW 4)
                resetTableIndex();
                for (int i = 27; i <= 34; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setEasinessData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getEasinessData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("EASINESS DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET RAPIDITY DATA (ROW 5)
                resetTableIndex();
                for (int i = 35; i <= 42; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setRapidityData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getRapidityData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("RAPIDITY DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET TACTICAL UTILITY DATA (ROW 6)
                resetTableIndex();
                for (int i = 43; i <= 50; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setTacticalUtilityData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getTacticalUtilityData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("TACTICAL UTILITY DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET FOOT MISTAKES DATA (ROW 7)
                resetTableIndex();
                for (int i = 51; i <= 58; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setFootMistakesData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getFootMistakesData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("FOOT MISTAKES DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET TORSO MISTAKES DATA (ROW 8)
                resetTableIndex();
                for (int i = 59; i <= 66; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setTorsoMistakesData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getTorsoMistakesData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("TORSO MISTAKES DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                //SET SLOW EXECUTION DATA (ROW 9)
                resetTableIndex();
                for (int i = 67; i <= 74; i++) {
                    //Double array 2D for playerData
                    try {
                        teamManager.getCurrentTeam().getCurrentPlayer().setSlowExecutionData(tableIndex, Double.valueOf(playerInfo[i]));
                        //System.out.println(data.getSlowExecutionData(i));
                        tableIndex++;
                    } catch (Exception e) {
                        System.out.println("SLOW EXECUTION DATA ERROR: " + e + " " + line + " at index: " + i);
                    }
                }
                tableIndex = 0; //reset the tableIndex
            }
        } catch (FileNotFoundException e) {
            System.out.println("File was not found in specfified location" + e);
        } catch (Exception e) {
            System.out.println("There was an error in parsing/reading the file: " + e);
        }

        teamManager.sortTeams();
        state.setChoosingFile(false);
        state.setFileImported();
        state.setFieldState();
    }

    /**
     * This method resets the value of Table Index
     */
    private void resetTableIndex() {
        tableIndex = 1;
    }
}
